// package: learn-go
// filename: nginxLog_demo
// author: diogoxiang@gmail.com
// time: 2023/12/11 15:45

package main

import (
	"fmt"
	"gitee.com/diogoxiang/learn-go/example/nginxLog_demo/nginxlog"
	"gitee.com/diogoxiang/learn-go/example/nginxLog_demo/sort"
	"log"
)

func main() {

	logFilePath := "E:\\gitee.com\\learn-go\\example\\nginxLog_demo\\data\\access.log"

	// 解析日志文件
	entries, err := nginxlog.ParseFile(logFilePath)
	if err != nil {
		log.Fatal(err)
	}

	// 统计 URL 访问次数
	urlCounts := nginxlog.CountURLs(entries)

	// 对 URL 进行排序
	sortedURLs := sort.SortURLs(urlCounts)

	// 输出访问次数最多的前 10 个 URL
	for i, urlCount := range sortedURLs {
		if i >= 10 {
			break
		}
		fmt.Printf("%s: %d\n", urlCount.URL, urlCount.Count)
	}

}
